package com.perfectoMobile.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jsystem.framework.JSystemProperties;
import jsystem.framework.report.ReportElement;
import jsystem.framework.report.Reporter;
import jsystem.framework.report.Reporter.ReportAttribute;
import junit.framework.SystemTestCase;

import org.apache.commons.httpclient.HttpStatus;

import com.perfectoMobile.utils.PmProperteis.ePmProperties;
import com.perfectoMobile.utils.device.DeviceFactory;
import com.perfectoMobile.utils.device.Dut;
import com.perfectoMobile.utils.device.GeneralEnums.IosVersion;
import com.perfectoMobile.utils.device.GeneralEnums.Os;
import com.perfectoMobile.utils.device.PmProduct;
import com.perfectoMobile.utils.device.androidDevices.AndroidDUT;
import com.perfectoMobile.utils.device.iosDevices.ios6Devices.Ios6DUT;
import com.perfectoMobile.utils.device.iosDevices.ios7Devices.Ios7DUT;
import com.perfectoMobile.utils.device.iosDevices.ios8Devices.Ios8DUT;
import com.perfectoMobile.utils.hss.Hss;
import com.perfectoMobile.utils.httpClient.HttpEngine;

public class ElUtils {

	public PmProperteis pmProp = null;
	public PmProduct device = null;
	public Map<String,Hss> hss = new HashMap<String, Hss>();
	public List<Dut> DutList  = null;
	public HttpEngine httpEngine = new HttpEngine();
	
	public ElUtils(){}
	public ElUtils(PmProduct device){ this.device = device ; }
	
	public void setup() throws Exception{		
		
		pmProp =  new PmProperteis(System.getProperty(ImportantStrings.USER_DIR) + File.separator + ImportantStrings.PM_PROPERTY_FILE); 
		DutList = initDuts();
		device  = DeviceFactory.getDeviceInstance(DutList.get(0));
	}
	
		
	public String getJsystemProperty(String key){	
		
		JSystemProperties p =JSystemProperties.getInstance();
		return p.getPreference(key);
	}
	
	
	public List<Dut>  initDuts() throws IOException{
		SystemTestCase.report.startLevel("initialize Duts with valid devices from pm.properties");

		String prop = PmProperteis.getProperty(ePmProperties.DEVICES);
		SystemTestCase.report.report("import devices from property file :- " +prop,ReportAttribute.BOLD);
		
		if(!prop.isEmpty() | prop!=null){
			List<Dut> devicesList = new ArrayList<Dut>();
			
			for(String deviceUrl : prop.split(",")){
				SystemTestCase.report.startLevel(deviceUrl);
				if(checkIsValidDevice(deviceUrl)){
						SystemTestCase.report.report(deviceUrl + " is valid device ");			
					devicesList.add(creatDeviceDUT(deviceUrl));											
				}
				else{					
					SystemTestCase.report.report(deviceUrl + " is not valid device ",Reporter.WARNING);
				}		
				SystemTestCase.report.stopLevel();
			}
			SystemTestCase.report.stopLevel();
			return devicesList;
		}
		else{
			SystemTestCase.report.report("No devices founded in Pm.properties file",false);
			System.err.println("No devices founded in Pm.properties file");
			}
		
		
		SystemTestCase.report.stopLevel();
														
		return null;
		
	}
	
	
	public Dut creatDeviceDUT(String deviceUrl) throws IOException{

		SystemTestCase.report.startLevel("create DUT");
		
				SystemTestCase.report.startLevel("metadata");
					 httpEngine.executeGetMethod(deviceUrl + "/camera/metadata.xml");
					 String metadata = httpEngine.getResponse();
					 SystemTestCase.report.report(metadata);
				 SystemTestCase.report.stopLevel();
				 
				SystemTestCase.report.startLevel("template");
					 httpEngine.executeGetMethod(deviceUrl + "/camera/template.xml");
					 String template = httpEngine.getResponse();
					 SystemTestCase.report.report(template);
				 SystemTestCase.report.stopLevel();
				 
				 
		
		Os os = Os.IOS;
		IosVersion iosVersion = IosVersion.NONE;


	switch(os)
	{
       
       case ANDROID:  
    	   Dut androidDut = new AndroidDUT(deviceUrl);
    	   break;  
    	   
       case IOS:
    	   
	    	   switch(iosVersion)
	    	   {
	    	   case IOS6:
	    		   Dut ios6Dut = new Ios6DUT(deviceUrl);
	    		   break;
	    	   case IOS7:
	    		   Dut ios7Dut = new Ios7DUT(deviceUrl);
	    		   break;
	    	   case IOS8:
	    		   Dut ios8Dut = new Ios8DUT(deviceUrl);
	    		   break;
	    	   
	    	   }
    	   
    	   break;
    	   
    	   default :
    		   
     }
	
		
      Dut d = new Dut(deviceUrl);	
		SystemTestCase.report.stopLevel();
		
		return d;
	}
	
	
	private boolean checkIsValidDevice(String deviceId) throws IOException{
		
		if(httpEngine.executeGetMethod(deviceId + "/" + "camera" + "/" + "status" )==HttpStatus.SC_OK)
		{
			String status = httpEngine.getResponse();
			SystemTestCase.report.startLevel("status for " +deviceId);
			SystemTestCase.report.report(status);
			SystemTestCase.report.stopLevel();
			
			if(status.contains("status result=\"OK\"")){
				return true;
			}
			else {
				return false;
			}
		}
		else{
			return false;
		}
		
		
	}
	
	public String  getHssIp(String deviceId){
		
		String IPADDRESS_PATTERN = 
		        "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";

		Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
		Matcher matcher = pattern.matcher(deviceId);
		        if (matcher.find()) {
		            return matcher.group();
		        }
		        else{
		            return "0.0.0.0";
		        }
		
	}
	
	public String getCradleId(String deviceId){
		
		String [] arr = deviceId.split("/");
		
     	return	(arr[arr.length-2]+"/"+arr[arr.length-1]);
		
	}
	
	
}
